package com.easygo.controller;

import com.easygo.service.IContentService;
import com.easygo.utils.PageUtils;
import com.xuexi.easygo.pojo.Content;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Scope("prototype")
public class ContentController {

    @Autowired
    IContentService contentService;

    @RequestMapping("content_page")
    public PageUtils<Content> getContent_Page(@RequestParam(defaultValue = "1",required = false) Integer pageIndex,
                                   @RequestParam(defaultValue = "5",required = false) Integer pageSize){
        Integer totalCount = contentService.getTotalCount();
        Map<String,Object> map = new HashMap<>();
        map.put("pageStart",(pageIndex-1)*pageSize);
        map.put("pageSize",pageSize);
        List<Content> contents = contentService.getContents(map);
        return new PageUtils<>(pageSize,pageIndex,totalCount,contents);
    }

    /*
    * 查询指定的广告
    * */
    @RequestMapping("content_getContentsByCategoryId")
    public List<Content> getContentsByCategoryId(Integer id){
        return contentService.getContentsByCategoryId(id);
    }
    /*
    * 修改广告的启用状态
    * */
    @RequestMapping("content_updateStatus")
    public Integer updateStatus(Integer id,Integer status){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("status",status);
        return contentService.updateStatus(map);
    }

    @RequestMapping("content_add")
    public Integer addContent(@RequestBody Content content){
        return contentService.addContent(content);
    }

    @RequestMapping("content_getContentById")
    public Content getContentById(@RequestParam(value = "id") Integer id){
        return contentService.getContentById(id);
    }

    @RequestMapping("content_update")
    public Integer updateContent(@RequestBody Content content){
        return contentService.updateContent(content);
    }
}
